const app = getApp()
import {
    util,
    Toast,
    Dialog,
    commonMixin
} from "../../utils/util.js";
Page({
    behaviors: [commonMixin],
    data: {
        id: 0,
        products: [], //推荐的商品
        list: [{
            id: 1,
            num: 1,
        }, ],
        result: [],
        checkGoods: [], //需要结算的商品数据
        totalPrice: 0, //金额
        showCoupon: false, //优惠码弹框
        couponCode: '', //优惠码
        frameCouponCode: '',
        showRemark: false, //备注弹框
        remark: '', //备注文案
        frameRemark: '',
        order: {},
        showLocationType: false,
        locationType: 0,
        locations: [],
        showLocation: false,
        locationTypeArray:[
          '门店检测', '上门检测'
        ],
        showLocationType: false
    },
    //复制订单号
    copy(e) {
        let copytxt = e.currentTarget.dataset.order;
        wx.setClipboardData({
            data: copytxt,
            success(res) {
            }
        })
    },

    selectLocationType() {
      this.setData({
        showLocationType: true
      });
    },

    onLocationTypeClose() {
      this.setData({
        showLocationType: false
      });
    },

    selectLocationTypeNode(e) {
      let index = e.currentTarget.dataset.index;
      this.setData({
          locationType: index,
          showLocationType: false,
      });
      this.modifyOrder();
    },

    onCloseRemark() {
        this.setData({
            showRemark: false,
            remark: this.data.frameRemark,
        })
        this.modifyOrder();
    },
    onOpenRemark() {
        this.setData({
            showRemark: true
        })
    },
    //输入优惠码弹框
    selectCoupon() {
        this.setData({
            showCoupon: true
        })
    },
    onCloseCoupon() {
        this.setData({
            showCoupon: false
        })
    },
    onLoad(options) {
        if (options.id) {
            this.setData({
                id: options.id
            });
            wx.showLoading({
                title: '加载中',
              })
              util.http.post("v1/mini/get-order",{
                id: options.id
              }).then((res) => {
                  if (res.data.code == 0) {
                    this.setData({
                        id: options.id,
                        order: res.data.data,
                        couponCode: res.data.data.coupon_code,
                        locationType: res.data.data.location_type
                    });
                    if (res.data.data.payment_status == 2 || res.data.data.payment_status == 6) {
                        wx.redirectTo({
                          url: '/pages/orderDatail/index?id=' + id,
                        })
                    } else if (res.data.data.payment_status == 3 || res.data.data.payment_status == 4 || res.data.data.payment_status == 5) {
                        wx.redirectTo({
                          url: '/pages/orderAll/index',
                        })
                    }
                  } else {
                    Toast(res.data.message);
                    wx.navigateBack();
                  }
                  wx.hideLoading();
              }).catch((err) => {
                  wx.hideLoading();
                  Toast('网络异常！刷新后重试！');
                  console.log(err)
              })
        }
        this.getInfoData();
    },
    onUnload: function () {
      console.log('shopCart onUnload')
    },
    onShow() {
        // let userInfo = util.getUserInfo();

    },

    selectLocation() {
      this.setData({
        showLocation: true,
      });
    },

    onLocationClose() {
      this.setData({
        showLocation: false,
      });
    },

    //获取详情数据
    getInfoData() {
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        var latitude = this.data.latitude;
        var longitude = this.data.longitude;
        wx.getLocation({
            type: 'wgs84',
            success: (res) => {
              latitude = res.latitude; // 纬度
              longitude = res.longitude; // 经度
              util.http.post("v1/mini/get-locations", {
                latitude: latitude,
                longitude: longitude
              }).then((res) => {
                res.data.data.list.sort((a, b) => a.distance - b.distance);
                this.setData({
                  locations: res.data.data.list
                })
              }).catch((err) => {
              })
            },
            fail: (err) => {
              util.http.post("v1/mini/get-locations", {
                latitude: latitude,
                longitude: longitude
              }).then((res) => {
                res.data.data.list.sort((a, b) => a.distance - b.distance);
                this.setData({
                  locations: res.data.data.list
                })
              }).catch((err) => {
              })
            }
        });
    },

    //去结算
    onClickButton() {

    },

    //改变了
    handleAmountChange(e) {
        // console.log(this.data.order.check_items[0].amount);
    },

    modifyOrder(failCallback) {
        util.http.post("v1/mini/modify-order",{
            order_no: this.data.order.order_no,
            amount: this.data.order.check_items[0].amount,
            check_item_id: this.data.order.check_items[0].check_item_id,
            coupon_code: this.data.couponCode,
            location_type: this.data.locationType,
            remark: this.data.remark,
          }).then((res) => {
              if (res.data.code == 0) {
                this.setData({
                    order: res.data.data
                });
              } else {
                Toast(res.data.message);
                failCallback();
              }
              wx.hideLoading();
          }).catch((err) => {
              wx.hideLoading();
              Toast('网络异常！刷新后重试！');
              console.log(err)
          })
    },

    submitData() {
        this.onCloseCoupon();
        this.setData({
            couponCode: this.data.frameCouponCode,
        });
        this.modifyOrder(()=>{
            this.setData({
                couponCode: '',
            });
        });
    },

    //减少
    handleAmountMinus(e) {
        let index =  e.currentTarget.dataset.index;
        let check_items = this.data.order.check_items;
        check_items[index].amount--;
        this.setData({
            order: this.data.order
        });
        this.modifyOrder();
    },
    //添加
    handleAmountPlus(e) {
        let index =  e.currentTarget.dataset.index;
        let check_items = this.data.order.check_items;
        check_items[index].amount++;
        this.setData({
            order: this.data.order
        });
        this.modifyOrder();
    },

    handleDoPay() {
        wx.showLoading({
            title: '支付中...',
        })

        var that = this;

        util.http.post("v1/mini/order-do-pay",{
            order_no: this.data.order.order_no
        }).then((res) => {
            if (res.data.code == 0) {
                let data = res.data.data;
                wx.requestPayment({
                    timeStamp: data.timeStamp,
                    nonceStr: data.nonceStr,
                    package: data.package,
                    signType: data.signType,
                    paySign: data.paySign,
                    success (rres) {
                        wx.hideLoading();
                        that.handlePayFinish();
                    },
                    fail (rres) { 
                        Toast('支付失败');
                        wx.hideLoading();
                    }
                })
            } else {
                Toast(res.data.message);
            }
        }).catch((err) => {
            wx.hideLoading();
            Toast('网络异常！刷新后重试！');
            console.log(err)
        })
    },

    handlePayFinish() {
        wx.showLoading({
          title: '加载中...',
        })
        util.http.post("v1/mini/pay-finish",{
            id: this.data.order.id
        }).then((res) => {
              if (res.data.code == 0) {
                Toast("支付成功");
                setTimeout(() => {
                    wx.redirectTo({
                        url: '/pages/orderDetail/index?id=' + this.data.order.id
                    })
                }, 1000)
              } else {
                Toast(res.data.message);
              }
              wx.hideLoading();
          }).catch((err) => {
              wx.hideLoading();
              Toast('网络异常！刷新后重试！');
              console.log(err)
          })
    },
    onReachBottom() {

    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
})